package ballpool;

import java.util.ArrayList;

import org.newdawn.slick.Color;

import game.GameMap;
import game.MovableGameObject;

public class Ball extends MovableGameObject {
	private Color color;
	private int startX, startY;
	private int modifier; //The amount of steps it takes to return a step back to the starting point
	
	public Ball(int x, int y) {
		super(x, y);
		startX = x;
		startY = y;
		modifier = (int) (Math.random()*500)+500;
		this.setMoveSpeed(4);
		this.setWidth(32);
		this.setHeight(32);
		this.setRotation(0);
		color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
	}
	
	public Color getColor(){
		return this.color;
	}
	
	@Override
	public void move(GameMap gameMap){
		if(canMoveWithoutGoingOutOfMap(gameMap.getWidth(), gameMap.getHeight())){
			this.addCoordinates((int)getXSpeed(), (int)getYSpeed());
		}else{
			this.setXSpeed(getXSpeed()*-1);
			this.setYSpeed(getYSpeed() *-1);
		}
		if(getXSpeed() > 0) setXSpeed(getXSpeed() - 0.1);
		if(getYSpeed() > 0) setYSpeed(getYSpeed() - 0.1);
		if(getXSpeed() < 0) setXSpeed(getXSpeed() + 0.1);
		if(getYSpeed() < 0) setYSpeed(getYSpeed() + 0.1);
	}
	
	public void step(GameMap gameMap, long stepnr){
		if(this.collidesWithMap(gameMap)){
				int[] collisionCoordinates = this.collidesWithMapOnPosition(gameMap);
				int halfWidth = this.getX()+(getWidth()/2);
				int halfHeight = this.getY()+(getHeight()/2);

				setXSpeed((halfWidth - collisionCoordinates[0])*0.25);
				setYSpeed((halfHeight - collisionCoordinates[1])*0.25);
		}
		if(stepnr%modifier == 0){
			if(this.getX() > startX){
				this.addCoordinates(-1, 0);
			}
			if(this.getX() < startX){
				this.addCoordinates(1, 0);
			}
			if(this.getY() > startY){
				this.addCoordinates(0, -1);
			}
			if(this.getY() < startY){
				this.addCoordinates(0, 1);
			}
		}
	}
}